package com.example.webflux.webflux.server;

import com.alibaba.fastjson.JSON;
import com.example.webflux.webflux.pojo.Student;
import com.example.webflux.webflux.service.StudentService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Collections;

/**
 *@Deacription TODO
 *@Author baohuajie
 *@Date 2020/12/30 18:26
 *@Version 1.0
 **/
@Component
public class Server {

    @Autowired
    private StudentService studentService;

    @Bean
    public RouterFunction<ServerResponse> queryAllTest() {
        RouterFunction<ServerResponse> route = RouterFunctions.route(RequestPredicates.POST("/test"), request -> {
            Flux<Student> reqParam = request.bodyToFlux(Student.class);
            reqParam.subscribe(c -> {
                String s = JSON.toJSONString(c);
                System.out.println(s);
            });
            Mono<ServerResponse> body = ServerResponse.ok().body(studentService.queryAll(), Student.class);
            return body;
        });

        return route;

    }

}
